package com.cognizant.ams.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cognizant.ams.bean.Ldmenu;
import com.cognizant.ams.bean.LdmenuExample;
import com.cognizant.ams.bean.Ldmenugrp;
import com.cognizant.ams.bean.LdmenugrpExample;
import com.cognizant.ams.dao.LdmenuMapper;
import com.cognizant.ams.dao.LdmenugrpMapper;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class MenuService {

	@Autowired
	private LdmenuMapper ldmenuMapper;
	
	@Autowired
	private LdmenugrpMapper tLdmenugrpMapper;

    /**
     * 根据用户查询菜单列表
     * @param username
     * @return
     */
	public List<Ldmenu> getUserMenu(String username){
		List<Ldmenu> list=ldmenuMapper.getUserMenu(username);
		return list;
	}

    /**
     * 查询所有有效菜单（页面默认加载）
     * @return
     */
	public List<Ldmenu> getAllMenu(){
		LdmenuExample menuExample =new LdmenuExample();
		LdmenuExample.Criteria criteria=menuExample.createCriteria();
		criteria.andNodesignEqualTo("1");//标志：1有效 0 无效
		List<Ldmenu> list=ldmenuMapper.selectByExample(menuExample);
		return list;
	}


    /**
     * 根据参数查询菜单
     * @param menu
     * @return
     */
	public List<Ldmenu> getMenu(Ldmenu menu){
        LdmenuExample example=new LdmenuExample();
        LdmenuExample.Criteria criteria=example.createCriteria();
        String nodecode=menu.getNodecode();
        String nodename=menu.getNodename();
        String nodesign=menu.getNodesign();

        if (!"".equals(nodecode)){
            criteria.andNodecodeEqualTo(nodecode);
        }
        if (!"".equals(nodename)){
            criteria.andNodenameEqualTo(nodename);
        }
        if (!"".equals(nodesign)){
            criteria.andNodesignEqualTo(nodesign);
        }
        List<Ldmenu> ldmenuList=ldmenuMapper.selectByExample(example);

        return  ldmenuList;
    }

    public int insertMenu(Ldmenu newMenu){
	    int result=ldmenuMapper.insert(newMenu);
	    return result;
    }

    public int deteteMenu(String nodecode){
	    LdmenuExample example=new LdmenuExample();
	    LdmenuExample.Criteria criteria=example.createCriteria();
	    criteria.andNodecodeEqualTo(nodecode);
	    int result=ldmenuMapper.deleteByExample(example);
	    return result;
    }


    public int updateMenu(Ldmenu menu){
        LdmenuExample example=new LdmenuExample();
        LdmenuExample.Criteria criteria=example.createCriteria();//创建更新的条件
        criteria.andNodecodeEqualTo(menu.getNodecode());
	    int result=ldmenuMapper.updateByExample(menu,example);
	    return result;
    }
	 
}
